﻿/*
 * Copyright (C) 2021. Huawei Device Co., Ltd. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the MIT License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * MIT License for more details.
 */

#ifndef DUILIB_UICULTURE_IMPLEMENT_H
#define DUILIB_UICULTURE_IMPLEMENT_H

#include "UICultureInterface.h"
#include <mutex>
#include <codecvt>

namespace DuiLib {

class UILIB_API UICultureImplement : public DuiLib::UICultureInterface {
public:
    UICultureImplement();
    ~UICultureImplement();

    void setResPath(LPCTSTR path) override;
    void setLocale(LPCTSTR locale, bool loadAll = true) override;

    LayoutDirection getDirection() override;
    CDuiString ParseBind(LPCTSTR pstrValue) override;
    CDuiString GetString(LPCTSTR key) override;
    CDuiString GetLocalString(LPCTSTR key) override;
    std::wstring GetLocaleLanguage() override;
    void SetCustomFont(wstring fontstr) override
    {
        m_customFont = fontstr;
    }

    wstring GetCustomFont() override
    {
        return m_customFont;
    }

    std::wstring GetMultiVarStr(const wstring &strIDS, const vector<int> &vecNum, int count=0) override;
    static wstring GetTextFromHtmlText(CPaintManagerUI* pManager, LPCTSTR pstrText, int iFont);
    CDuiString GetMultiVarString(LPCTSTR originalString, ...) override;
    static int32_t UnicodeFormat(const string &pstrFormat, wstring &unicodeChar, va_list vaList);
    std::wstring GetPercentageStr(const int percentage) override;
    std::wstring ToUpper(const wstring &src) override;
    const std::wstring& GetResPath();
    std::wstring NumberLocalFormat(double value) override;

private:
    void UploadAllResourceBundle();
    CDuiString GetAllString(LPCTSTR key);
    CDuiString AddAllText(LPCTSTR key, CDuiString text);
    std::wstring GetLocalMultiVarStr(const wstring &strIDS, const vector<int> &vecNum, int count = 0);
    std::wstring GetPluralStr(const wstring &language, const wstring &value, const vector<int> &vecNum, int count = 0);
    struct Core;
    std::unique_ptr<Core> core_;
    wstring m_customFont;
    wstring resPath = L"";
    bool isSupportCurLang = true;
    std::once_flag callflag;
    bool checkLanguage = false;
    bool useEnUs = false;
    std::shared_ptr<void> countryRes;
    std::shared_ptr<void> countryRb;
    std::shared_ptr<void> rootRes;
    std::shared_ptr<void> rootRb;
    bool notInitialize = true;
    void* rootStrings = nullptr;
    void* countryStrings = nullptr;
    std::wstring_convert<std::codecvt_utf8_utf16 <wchar_t>> m_converter;
};

} // namespace DuiLib

#endif // !DUILIB_UICULTURE_IMPLEMENT_H
